package com.recommend.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.github.pagehelper.PageHelper;

/**
 * Created by lw.
 */
@org.springframework.context.annotation.Configuration
@EnableTransactionManagement
@MapperScan(basePackages = DatabaseConfig.DAOPACKAGE,sqlSessionFactoryRef = "sqlSessionFactory")
public class DatabaseConfig {
    private String username = "admin";

    private String password ="admin";
    private String url ="jdbc:mysql://127.0.0.1:3306/movie?characterEncoding=UTF-8";

    private String driverClass ="com.mysql.jdbc.Driver";
    public static final String DAOPACKAGE = "com.recommend.persistence";
    public static final String MAPPERPACKAGE = "classpath*:recommend/*.xml";
    public static final String DOMAINPACKAGE = "com.recommend.domain";
//    @Resource
//    private Interceptor pageInterceptor;


    @Bean
    @Primary
    public DataSource dataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setUsername(username);
        driverManagerDataSource.setPassword(password);
        driverManagerDataSource.setUrl(url);
        driverManagerDataSource.setDriverClassName(driverClass);
        return driverManagerDataSource;
    }

    @Bean
    @Primary
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setPlugins(new Interceptor[] {interceptor()});
        sessionFactory.setTypeAliasesPackage(DOMAINPACKAGE);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setMapperLocations(resolver.getResources(MAPPERPACKAGE));
        SqlSessionFactory factory = sessionFactory.getObject();
		setConfigurationProperties(factory.getConfiguration());
        return factory;
    }
  //添加分页拦截器
  	public static Interceptor interceptor() {
  		Interceptor interceptor = new PageHelper();
  		Properties properties = new Properties();
  		properties.put("dialect","mysql");
  		properties.put("reasonable",true);
  		interceptor.setProperties(properties);
  		return interceptor;
  	}

//    设置等待时间
	private void setConfigurationProperties(Configuration configuration) {
			configuration.setLazyLoadingEnabled(true);
			configuration.setDefaultStatementTimeout(1000);
	}

	//事务配置
	@Bean(name="transactionManager")
	@Primary
	public PlatformTransactionManager crmTransactionManager() {
		return new DataSourceTransactionManager(dataSource());
	}
	
	public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDriverClass() {
        return driverClass;
    }

    public void setDriverClass(String driverClass) {
        this.driverClass = driverClass;
    }

//    public void setPageInterceptor(Interceptor pageInterceptor) {
//        this.pageInterceptor = pageInterceptor;
//    }
//
//    public Interceptor getPageInterceptor() {
//        return pageInterceptor;
//    }
}
